﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InfoLayer;
using ControlLayer;

namespace GUI
{
    public partial class frmLapDanhMucPhong : Form
    {
        private PhongControl phongControl;
        private PhongInfo phongInfo;
        private LoaiPhongControl loaiPhongControl;
        private LoaiPhongInfo loaiPhongInfo;

        public frmLapDanhMucPhong()
        {
            InitializeComponent();
            phongInfo = new PhongInfo();
            phongControl = new PhongControl();
            loaiPhongControl = new LoaiPhongControl();
            loaiPhongInfo = new LoaiPhongInfo();
        }

        private void frmLapDanhMucPhong_Load(object sender, EventArgs e)
        {
            DataTable _dt;
            _dt = loaiPhongControl.LayBangDanhSachLoaiPhong();
            comboBoxMaLoaiPhong.DataSource = _dt;
            comboBoxMaLoaiPhong.DisplayMember = "TenLoaiPhong";
            comboBoxMaLoaiPhong.ValueMember = "MaLoaiPhong";
            comboBoxMaLoaiPhong.SelectedIndex = 0;
            
            _dt = phongControl.LayBangDanhSachPhong();
            if (_dt.Rows.Count > 0)
                textBoxMaPhong.Text = phongControl.TaoMaTuDong(_dt.Rows[_dt.Rows.Count - 1][0].ToString(), 2);
            else
                textBoxMaPhong.Text = phongControl.TaoMaTuDong("MP000", 2);

            textBoxTinhTrangPhong.Text = "false";

            DataTable dt;
            dt = phongControl.LayBangDanhSachPhong();
            Load_DanhSach_Phong_ListView(dt);

            //string kq = "";
            //string s = "MPH0011";
            //string str = s.Substring(3);
            //kq = s.Substring(0, 3);
            //textBoxGhiChu.Text = str;// kq;// s.Substring(1);

            //int so = Convert.ToInt32(str) + 1;
            //for (int i = 0; i < str.Length - so.ToString().Length; i++)
            //    kq += "0";
            //kq = kq + so.ToString();
            //textBoxGhiChu.Text = kq;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            DataTable dt;
            dt = phongControl.LayBangDanhSachPhong();

            if (textBoxTenPhong.Text.Trim().Length == 0)
            {
                MessageBox.Show("Vui lòng nhập tên phòng.");
                return;
            }

            if (dt.Rows.Count > 0)
                phongInfo.MaPhong = phongControl.TaoMaTuDong(dt.Rows[dt.Rows.Count - 1][0].ToString(), 2);
            else
                phongInfo.MaPhong = phongControl.TaoMaTuDong("MP000", 2);

            //Kiem tra Ten Phong da duoc lap hay chua?
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (textBoxTenPhong.Text == dt.Rows[i][1].ToString())
                {
                    MessageBox.Show("Tên phòng đã được lập.", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            phongInfo.TenPhong = textBoxTenPhong.Text;
            phongInfo.MaLoaiPhong = comboBoxMaLoaiPhong.SelectedValue.ToString(); //Lay gia tri thuc cua combobox S
            phongInfo.TinhTrangPhong = false;// false: chua thue, true: da duoc thue
            phongInfo.GhiChu = textBoxGhiChu.Text;
                    
            //xu li lay don gia
            dt = phongControl.LayDonGia(loaiPhongInfo, phongInfo.MaLoaiPhong);
            textBoxDonGia.Text = dt.Rows[0][0].ToString();

            phongControl.Insert(ref phongInfo);

            //Cap nhat lai danh sach phong
            dt = phongControl.LayBangDanhSachPhong();
            Load_DanhSach_Phong_ListView(dt);

            //Khong cho nhap Button Them nua
            btnThem.Enabled = false;
        }

        private void btnLammoi_Click(object sender, EventArgs e)
        {
            textBoxMaPhong.Clear();
            textBoxTenPhong.Clear();
            comboBoxMaLoaiPhong.SelectedIndex = 0;
            textBoxDonGia.Clear();
            textBoxGhiChu.Clear();
            textBoxTinhTrangPhong.Text = "false";
            btnThem.Enabled = true;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Load_DanhSach_Phong_ListView(DataTable tbPhong)
        {
            ListViewItem item;
            listViewDSPhong.Items.Clear();

            for (int i = 0; i < tbPhong.Rows.Count; i++ )
            {
                item = listViewDSPhong.Items.Add(tbPhong.Rows[i][0].ToString());
                for (int j = 1; j < tbPhong.Columns.Count; j++)
                    item.SubItems.Add(tbPhong.Rows[i][j].ToString());
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            phongInfo.MaPhong = textBoxMaPhong.Text;
            phongInfo.TenPhong = textBoxTenPhong.Text;
            phongInfo.MaLoaiPhong = comboBoxMaLoaiPhong.SelectedValue.ToString();// SelectedItem.ToString(); 
            phongInfo.TinhTrangPhong = Convert.ToBoolean(textBoxTinhTrangPhong.Text);
            phongInfo.GhiChu = textBoxGhiChu.Text;

            if (phongInfo.TinhTrangPhong == true)
            {
                MessageBox.Show("Phòng đang được thuê, không thể update.", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            phongControl.Update(phongInfo);

            //Cap nhat danh sach phong
            DataTable dt;
            dt = phongControl.LayBangDanhSachPhong();
            Load_DanhSach_Phong_ListView(dt);
        }

        private void listViewDSPhong_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = listViewDSPhong.FocusedItem.Index;
            if (i < 0)
                return;
            textBoxMaPhong.Text = listViewDSPhong.Items[i].Text;
            textBoxTenPhong.Text = listViewDSPhong.Items[i].SubItems[1].Text;

            DataTable dt = loaiPhongControl.LayBangDanhSachLoaiPhong();
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                if (listViewDSPhong.Items[i].SubItems[2].Text == dt.Rows[j][0].ToString())
                    comboBoxMaLoaiPhong.Text = dt.Rows[j][1].ToString();
            }
            //comboBoxMaLoaiPhong.Text = listViewDSPhong.Items[i].SubItems[2].Text;
            textBoxTinhTrangPhong.Text = listViewDSPhong.Items[i].SubItems[3].Text;
            textBoxGhiChu.Text = listViewDSPhong.Items[i].SubItems[4].Text; 
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            phongInfo.MaPhong = textBoxMaPhong.Text;
            phongInfo.TenPhong = textBoxTenPhong.Text;
            phongInfo.MaLoaiPhong = comboBoxMaLoaiPhong.SelectedValue.ToString();// SelectedItem.ToString(); 
            phongInfo.TinhTrangPhong = Convert.ToBoolean(textBoxTinhTrangPhong.Text);
            phongInfo.GhiChu = textBoxGhiChu.Text;

            if (phongInfo.TinhTrangPhong == true)
            {
                MessageBox.Show("Phòng đang được thuê, không thể xóa.", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            phongControl.Delete(phongInfo);

            //Cap nhat danh sach phong
            DataTable dt;
            dt = phongControl.LayBangDanhSachPhong();
            Load_DanhSach_Phong_ListView(dt);
        }

        private void comboBoxMaLoaiPhong_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt;
            //xu li lay don gia
            phongInfo.MaLoaiPhong = comboBoxMaLoaiPhong.SelectedValue.ToString();
            dt = phongControl.LayDonGia(loaiPhongInfo, phongInfo.MaLoaiPhong);
            if (dt.Rows.Count > 0)
                textBoxDonGia.Text = dt.Rows[0][0].ToString();
            else
                return;
        }

        

         
    }
}
